
from app.models.admin import Admin
from app.models.auth import Auth
from app.models.adminlog import Adminlog
from app.models.comment import Comment
from app.models.movie import Movie
from app.models.moviecol import Moviecol
from app.models.oplog import Oplog
from app.models.preview import Preview
from app.models.role import Role
from app.models.tag import Tag
from app.models.user import User
from app.models.userlog import Userlog


from typing import Any
from datetime import datetime

from sqlalchemy import Column, Integer, DateTime
from sqlalchemy.ext.declarative import as_declarative, declared_attr




@as_declarative()
class Base:
    # 模型基类
    id: Any
    create_time = Column(DateTime, default=datetime.now, onupdate=datetime.now)
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now)
    status = Column(Integer, default=1)  # 0为删除

    def delete(self):
        self.status = 0

    @declared_attr
    def __tablename__(cls) -> str:
        return cls.__name__.lower()


from sqlalchemy import create_engine
engine = create_engine(
        SQLALCHEMY_DATABASE_URI,
        max_overflow=0,  # 超过连接池大小外最多创建的连接
        pool_size=5,  # 连接池大小
        pool_timeout=30,  # 池中没有线程最多等待的时间，否则报错
        pool_recycle=-1  # 多久之后对线程池中的线程进行一次连接的回收（重置）
    )

# 数据库会话
def create_session():
    return sessionmaker(autocommit=False, autoflush=False, bind=engine)
session = create_session()